import axios from "axios"
import ContentType from "./ContentType";
let { VITE_APP_API_ROOT, VITE_APP_API_PREFIX } = import.meta.env

const instance = axios.create({
    baseURL: VITE_APP_API_ROOT + VITE_APP_API_PREFIX,

    timeout: 3000,
    headers: {
        'content-type': ContentType.json
    }
});
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    return response;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

async function postHeadler(url: string, data: any, params: any, options: object) {
    return await instance.post(url, data, { params: params, ...options })
}
async function getHeadler(url: string, params: any, options: object) {
    return await instance.get(url, { params: params, ...options })
}
async function delHeadler(url: string, params: any, options: object) {
    return await instance.delete(url, { params: params, ...options })
}
export default {
    post: postHeadler,
    get: getHeadler,
    del: delHeadler
}